/*
 * Copyright 2016-2017 TVI Go Easy.
 * Created on 2017/4/29 15:11
 */
package org.mechanic.fund.service.product.impl;

import org.mechanic.core.Constants;
import org.mechanic.fund.domain.system.manager.Permission;
import org.mechanic.fund.domain.utils.mapper.DtoBeanMapper;
import org.mechanic.fund.repository.system.PermissionRepository;
import org.mechanic.fund.service.product.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 权限信息服务实现
 *
 * @author mechanic
 * @version 0.0.1
 */
@Service
public class PermissionService implements IPermissionService {

    private final PermissionRepository permissionRepository;

    @Autowired
    public PermissionService(PermissionRepository permissionRepository) {this.permissionRepository = permissionRepository;}

    /**
     * 返回匹配权限编号的权限信息
     *
     * @param id 权限编号
     * @return 权限信息
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Permission findById(String id) {
        return this.permissionRepository.findOne(id);
    }

    /**
     * 返回权限信息分页列表
     *
     * @param pageRequest 分页信息
     * @return 权限信息分页列表
     */
    @Override
    @Cacheable(value = Constants.EHCACHE_NAME)
    public Page<Permission> getList(PageRequest pageRequest) {
        return this.permissionRepository.findAll(pageRequest);
    }

    /**
     * 返回匹配权限编号的权限信息分页列表
     *
     * @param roleId 权限编号
     * @param pageRequest 分页信息
     * @return 权限信息分页列表
     */
    //@Override
    //public Page<Permission> getList(String roleId, PageRequest pageRequest) {
    //    return null;
    //}

    /**
     * 删除匹配权限编号的权限信息
     *
     * @param id 权限编号
     */
    @Override
    public void delete(String id) {
        this.permissionRepository.delete(id);
    }

    /**
     * 保存权限信息
     *
     * @param permissions 权限信息集合
     */
    @Override
    public void save(List<Permission> permissions, String operationUserId) {
        this.permissionRepository.save(DtoBeanMapper.setOperationToList(permissions, operationUserId));
    }
}
